package com.ims.core.saleconfig.service.impl;

import java.util.List;

import com.alibaba.fastjson2.util.UUIDUtils;
import com.ims.common.annotation.RequestParamsSplit;
import com.ims.common.constant.Constants;
import com.ims.common.utils.DateUtils;
import com.ims.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ims.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ims.core.saleconfig.mapper.TSalBuyerMapper;
import com.ims.core.saleconfig.domain.TSalBuyer;
import com.ims.core.saleconfig.service.ITSalBuyerService;
import org.springframework.util.CollectionUtils;

/**
 * 购方Service业务层处理
 *
 * @author IMS
 * @date 2024-05-19
 */
@Service
public class TSalBuyerServiceImpl implements ITSalBuyerService
{
    @Autowired
    private TSalBuyerMapper tSalBuyerMapper;

    /**
     * 查询购方
     *
     * @param id 购方主键
     * @return 购方
     */
    @Override
    public TSalBuyer selectTSalBuyerById(Long id)
    {
        return tSalBuyerMapper.selectTSalBuyerById(id);
    }

    /**
     * 购方税号查询购方
     *
     * @param buyerNumber 购方税号
     * @return 购方
     */
    @Override
    public TSalBuyer selectTSalBuyerByNumber(String buyerNumber)
    {
        return tSalBuyerMapper.selectTSalBuyerByNumber(buyerNumber);
    }

    /**
     * 查询购方列表
     *
     * @param tSalBuyer 购方
     * @return 购方
     */
    @Override
    @RequestParamsSplit
    public List<TSalBuyer> selectTSalBuyerList(TSalBuyer tSalBuyer)
    {
        return tSalBuyerMapper.selectTSalBuyerList(tSalBuyer);
    }

    /**
     * 新增购方
     *
     * @param tSalBuyer 购方
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertTSalBuyer(TSalBuyer tSalBuyer)
    {
        tSalBuyer.setBuyerId(IdUtils.simpleUUID());
        tSalBuyer.setCreateTime(DateUtils.getNowDate());
        tSalBuyer.setIsEnabledName(tSalBuyer.getIsEnabled() == 1 ? "失效":"有效" );
        int rows = tSalBuyerMapper.insertTSalBuyer(tSalBuyer);
        return rows;
    }

    /**
     * 修改购方
     *
     * @param tSalBuyer 购方
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateTSalBuyer(TSalBuyer tSalBuyer)
    {
        tSalBuyer.setUpdateTime(DateUtils.getNowDate());
        tSalBuyer.setIsEnabledName(tSalBuyer.getIsEnabled() == 1 ? "失效":"有效" );
        return tSalBuyerMapper.updateTSalBuyer(tSalBuyer);
    }

    /**
     * 批量删除购方
     *
     * @param ids 需要删除的购方主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteTSalBuyerByIds(Long[] ids)
    {
        return tSalBuyerMapper.deleteTSalBuyerByIds(ids);
    }

    /**
     * 删除购方信息
     *
     * @param id 购方主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteTSalBuyerById(Long id)
    {
        TSalBuyer tSalBuyer = tSalBuyerMapper.selectTSalBuyerById(id);
        return tSalBuyerMapper.deleteTSalBuyerById(id);
    }

    @Override
    public void syncBuyerByErp(List<TSalBuyer> tSalBuyerList) {
        TSalBuyer tSalBuyerCondition = new TSalBuyer();
        for(TSalBuyer tSalBuyer : tSalBuyerList){
            tSalBuyerCondition.setBuyerId(tSalBuyer.getBuyerId());
            List<TSalBuyer> orgTSalBuyerList =  tSalBuyerMapper.selectTSalBuyerList(tSalBuyerCondition);
            if(!CollectionUtils.isEmpty(orgTSalBuyerList)){
                // 已存在，更新其他信息
                TSalBuyer orgTSalBuyer = orgTSalBuyerList.get(0);
                orgTSalBuyer.setBuyerTel(tSalBuyer.getBuyerTel());
                orgTSalBuyer.setBuyerCode(tSalBuyer.getBuyerCode());
                orgTSalBuyer.setBuyerName(tSalBuyer.getBuyerName());
                orgTSalBuyer.setBuyerAccount(tSalBuyer.getBuyerAccount());
                orgTSalBuyer.setBuyerAddress(tSalBuyer.getBuyerAddress());
                orgTSalBuyer.setBuyerNumber(tSalBuyer.getBuyerNumber());
                orgTSalBuyer.setUpdateBy("erp");
                orgTSalBuyer.setUpdateTime(DateUtils.getNowDate());
                tSalBuyerMapper.updateTSalBuyer(orgTSalBuyer);
            } else {
                tSalBuyerMapper.insertTSalBuyer(tSalBuyer);
            }
        }
    }
}
